#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    pid_t id = fork();
    if(id == 0)
    {
        std::cout << "我是子进程" << std::endl;
        char* const envp[] = {\
            (char*)"HELLO=KEY"\
            ,(char*)nullptr};
        char *const argv[] = {
            (char*)"ls",
            (char*)"--color",
            (char*)"-la",
            (char*) nullptr
        };
        execv("/bin/ls",argv);
        //execle("/usr/bin/env","env",nullptr,envp);
        //execle("/usr/bin/env","env",nullptr,envp);

        //execlp("./other","./other",nullptr);
        //std::cout << std::endl;
        // execlp("ls","ls","-a","-l",nullptr);//执行完进程替换之后，该进程就会退出，不会执行之后的代码，因为原进程的上下文数据被新进程替换,也就是被清空了
        //execlp("bash","bash","test.sh",nullptr);
        // execlp("python3","python3","hello.py",nullptr);



        //execl("/bin/bash","bash","test.sh",nullptr);
        //execl("/usr/bin/python3","python3","hello.py",nullptr);
        //execl("./other","other",nullptr);
        //execl("/bin/ls","ls","-a","-l",nullptr);
        //execl("/bash","cd","..",nullptr);
        exit(0);
    }
    pid_t wid = waitpid(id,nullptr,0);
    if(wid > 0)
    {
        std::cout << "等待子进程成功" << std::endl;
    }
    else
    {
        std::cout << "等待子进程失败" << std::endl;
    }
    return 0;
}
